<template>
  <div>
    <el-card>
      <div class="header" slot="header">
        <span>文章分类</span>
        <el-button type="primary" @click="addDialog = true">添加分类</el-button>
      </div>
      <el-table :data="list" ref="form" border stripe style="width: 100%">
        <el-table-column prop="id" label="ID" width="180"> </el-table-column>
        <!-- 不给宽度就平均分配 -->
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button size="mini" @click="showEditDialog(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加对话框 -->
    <el-dialog title="添加分类" :visible.sync="addDialog" :before-close="cancel" width="500px">
      <el-form label-width="80px" ref="addform" :model="addData" :rules="rules">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input autocomplete="off" v-model="addData.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input autocomplete="off" v-model="addData.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="postCate">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑对话框 -->
    <el-dialog title="添加分类" :visible.sync="editDialog" width="500px">
      <el-form label-width="80px" ref="editform" :model="editDate" :rules="rules">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editDate.cate_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editDate.cate_alias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialog = false">取 消</el-button>
        <el-button type="primary" @click="updateCategory">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getListAPI, postCateAPI, delIdAPU, updateCategoryAPI } from '@/api/category'

export default {
  name: 'CateList',
  data() {
    return {
      list: [],
      // 控制添加弹框的数据项
      addDialog: false,
      // 控制编辑的弹框数据项
      editDialog: false,
      // 收集添加表单的数据
      addData: {
        cate_name: '',
        cate_alias: ''
      },
      editDate: {
        cate_name: '',
        cate_alias: '',
        id: ''
      },

      rules: {
        cate_name: [
          { required: true, message: '分类名称必填', trigger: 'blur' },
          { min: 1, max: 10, message: '长度1~10位', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '分类别名必填', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '长度在 1 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async getList() {
      const { data: res } = await getListAPI()
      if (res.code === 0) {
        // 提示成功
        // 将获取成功的值赋值给list
        // this.$message.success(res.message)
        this.list = res.data
      }
    },
    postCate() {
      this.$refs.addform.validate(async val => {
        if (!val) return
        const { data: res } = await postCateAPI(this.addData)
        if (res.code === 0) {
          // 提示添加成功
          // 将添加的数据，添加到list中
          this.$message.success(res.message)
          this.getList()
          // 重置表单
          this.$refs.addform.resetFields()
          // 关闭弹框
          this.addDialog = false
        } else {
          this.$message.error(res.message)
        }
      })
    },
    cancel() {
      // 重置表单
      this.$refs.addform.resetFields()
      this.addDialog = false
    },
    del(id) {
      // console.log(id)
      this.$confirm('确定删除吗?', '提示', {
        type: 'warning',
        callback: async obj => {
          // obj的值可能时confirm，cancel，close
          if (obj === 'confirm') {
            const { data: res } = await delIdAPU(id)
            if (res.code === 0) {
              this.$message.success(res.message)
              this.getList()
            } else {
              this.$message.error(res.message)
            }
          }
        }
      })
    },
    showEditDialog(row) {
      this.editDialog = true
      this.editDate = { ...row }
    },
    updateCategory() {
      this.$refs.editform.validate(async val => {
        if (!val) return
        const { data: res } = await updateCategoryAPI(this.editDate)
        if (res.code === 0) {
          this.$message.success(res.message)
          this.getList()
          this.editDialog = false
        } else {
          this.$message.error(res.message)
        }
      })
    }
  },
  created() {
    this.getList()
  }
}
</script>

<style lang="less" scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
